#include "recboxitem.h"
#include "ui_recboxitem.h"

RecBoxItem::RecBoxItem(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::RecBoxItem)
{
    ui->setupUi(this);
    ui->musicImageBox->installEventFilter(this);//安装事件拦截器
}

RecBoxItem::~RecBoxItem()
{
    delete ui;
}

void RecBoxItem::setText(const QString &text)
{
    ui->recBoxItemText->setText(text);
}

void RecBoxItem::setImage(const QString &imagePath)
{
    QString imgStyle = "background-image:url("+imagePath+");";
    ui->recMusicImage->setStyleSheet(imgStyle);
}

bool RecBoxItem::eventFilter(QObject *watched, QEvent *event)
{
    if(watched == ui->musicImageBox)//
    {
        //先获取控件的矩形大小
        int width = ui->musicImageBox->width();
        int height = ui->musicImageBox->height();
        if(event->type() == QEvent::Enter)//如果进入图片，那么开始执行动画
        {
            //第一步先定义动画对象
            QPropertyAnimation* animation = new QPropertyAnimation(ui->musicImageBox,"geometry");//不绑定到对象树，自己释放
            //设置动画时间
            animation->setDuration(100);
            //设置关键帧
            animation->setStartValue(QRect(5,9,width,height));
            animation->setEndValue(QRect(5,0,width,height));
            //启动动画
            animation->start();
            //捕捉结束信息
            connect(animation,&QPropertyAnimation::finished,this,[=](){
                delete animation;
                //qDebug()<<"图片移动动画结束";
            });
        }
        else if(event->type() == QEvent::Leave)
        {
            //第一步先定义动画对象
            QPropertyAnimation* animation = new QPropertyAnimation(ui->musicImageBox,"geometry");//不绑定到对象树，自己释放
            //设置动画时间
            animation->setDuration(100);
            //设置关键帧
            animation->setStartValue(QRect(5,0,width,height));
            animation->setEndValue(QRect(5,9,width,height));
            //启动动画
            animation->start();
            //捕捉结束信息
            connect(animation,&QPropertyAnimation::finished,this,[=](){
                delete animation;
                //qDebug()<<"图片移动动画结束";
            });
        }
        return true;
    }
    return QObject::eventFilter(watched,event);
}
